package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.other.BaseController;
import com.example.domain.Employee;
import com.example.result.JsonResult;
import com.example.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 *  员工表
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController {   //员工表
    @Autowired
    private EmployeeService employeeService;

    //新增员工
    @PostMapping("/save")
    public JsonResult save(@RequestBody Employee employee,HttpSession session){
        employee.setStatus(1);
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getName,employee.getName());
        Employee result = employeeService.getOne(queryWrapper);
        if(result!=null){
            return new JsonResult(0,"员工已存在!");
        }
        System.out.println(employee.toString());
        boolean flag = employeeService.save(employee);
        if(!flag){
            return new JsonResult(0,"添加失败，请重试!");
        }
        return new JsonResult(1,"新增成功");
    }
    //员工列表分页查询
    @GetMapping("/page")
    public JsonResult page(Integer currentPage,Integer pageSize,String name){
        if(currentPage==null||pageSize==null){
            return new JsonResult(0,"当前页或者当前最大页数错误");
        }
        IPage<Employee> iPage=new Page<>(currentPage,pageSize);      //当前显示页数，每页所支持的最大条数
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);     //根据时间排序
        employeeService.page(iPage,queryWrapper);
        //System.out.println(iPage.);
        return new JsonResult(1,"",iPage);
    }
    //禁用或启用员工按钮
    @PostMapping("/setStatus")
    public JsonResult setStatus(@RequestBody Employee employee,HttpSession session){
        if(employee.getStatus()==1){
            employee.setStatus(0);
        }else{
            employee.setStatus(1);
        }
        boolean flag = employeeService.updateById(employee);
        if(!flag){
            return new JsonResult(0,"修改失败,请重试!");
        }
        return new JsonResult(1,"操作成功");
    }
    //修改员工信息
    @PutMapping("/update")
    public JsonResult update(@RequestBody Employee employee){
        boolean flag = employeeService.updateById(employee);
        if(!flag){
            return new JsonResult(0,"修改失败,请刷新!");
        }
        return new JsonResult(1,"信息修改成功");
    }
    //根据id查询员工信息
    @GetMapping("/gainByID/{id}")
    public JsonResult getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee==null){
            return new JsonResult(0,"员工信息不存在，请刷新!");
        }
        return new JsonResult(1,"",employee);
    }
}
